package com.example.reeman.ymfacedemo.readface;

import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;

import com.example.reeman.ymfacedemo.MainActivity;

public class FloatWindowManager {

    private static WindowManager mWindowManager;


    private static ReadfaceView mReadfaceView;
    private static WindowManager.LayoutParams mParams;
    private static WindowManager windowManager;
    public static Context mContext;
    public static void createReadfaceWindow(Context context) {
        Log.v("FloatWindowManager", "mReadfaceView");
        mContext = context;
        windowManager = getWindowManager(context);
        if (mReadfaceView == null) {
            mReadfaceView = new ReadfaceView(context);
            mParams = new WindowManager.LayoutParams();
            mParams.x = 1200;
            mParams.y = 0;
            mParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
            mParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_FULLSCREEN;
            mParams.format = PixelFormat.RGBA_8888;
            mParams.width = 300;
            mParams.height = 220;
            mParams.gravity = Gravity.LEFT | Gravity.TOP;
            windowManager.addView(mReadfaceView, mParams);
            mReadfaceView.onResume();
        }
        mTouchStartX = mTouchStartY = preRawX = preRawY = y = 0;
        x = 1200;
        initTouchEvent();
    }
    /**
     * 设置悬浮窗监听事件
     * by melon
     */
    static float preRawX=0;
    static float preRawY=0;
    static float curRawX=0;
    static float curRawY=0;
    private static boolean initViewPlace = false;
    private static float mTouchStartX;
    private static float mTouchStartY;
    private static float x;
    private static float y;
    private static void initTouchEvent() {
        SurfaceView camera_view = mReadfaceView.getCamera_view();
        if (camera_view != null) {
            camera_view.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            if (!initViewPlace) {
                                initViewPlace = true;
                                //获取初始位置
                                mTouchStartX += (event.getRawX() - mParams.x);
                                mTouchStartY += (event.getRawY() - mParams.y);
                            } else {
                                //根据上次手指离开的位置与此次点击的位置进行初始位置微调
                                mTouchStartX += (event.getRawX() - x);
                                mTouchStartY += (event.getRawY() - y);
                            }
                            preRawX=event.getRawX();
                            preRawY=event.getRawY();
                            break;
                        case MotionEvent.ACTION_MOVE:
                            // 获取相对屏幕的坐标，以屏幕左上角为原点
                            x = event.getRawX();
                            y = event.getRawY();
                            updateViewPosition();
                            break;

                        case MotionEvent.ACTION_UP:
                            curRawX = event.getRawX();
                            curRawY = event.getRawY();
                            if (preRawY == curRawY & preRawX == curRawX) {
                                //点击事件
//                                mContext.startActivity(new Intent(mContext, MainActivity.class));
                            }
                            break;
                    }
                    return true;
                }
            });
        }
    }
    /**
     * 更新浮动窗口位置
     */
    private static void updateViewPosition() {
        mParams.x = (int) (x - mTouchStartX);
        mParams.y = (int) (y - mTouchStartY);
        windowManager.updateViewLayout(mReadfaceView, mParams);
    }
    public static void removeReadfaceView(Context context) {
        if (mReadfaceView != null) {
            Log.v("FloatWindowManager", "removemReadfaceView");
            mReadfaceView.onPause();
            getWindowManager(context).removeView(mReadfaceView);
            mReadfaceView = null;
        }
    }

    public static void removeAllView(Context context) {
        removeReadfaceView(context);
    }

    /**
     * 获取windowmanager
     *
     * @param context
     * @return
     */
    public static WindowManager getWindowManager(Context context) {
        if (mWindowManager == null) {
            mWindowManager = (WindowManager) context.getApplicationContext()
                    .getSystemService(Context.WINDOW_SERVICE);
        }
        return mWindowManager;
    }

//    private static ReemanFaceAIView faceAiView;
//
//
//    public static void removeAllView(Context context) {
//        removeReemacFaceAIView(context);
//    }
//
//    /**
//     * 获取windowmanager
//     *
//     * @param context
//     * @return
//     */
//    public static WindowManager getWindowManager(Context context) {
//        if (mWindowManager == null) {
//            mWindowManager = (WindowManager) context.getApplicationContext()
//                    .getSystemService(Context.WINDOW_SERVICE);
//        }
//        return mWindowManager;
//    }
//
//    public static void createReemanFaceAIWindow(Context context) {
//        Log.v("FloatWindowManager", "createReemanfaceWindow");
//        WindowManager windowManager = getWindowManager(context);
//        if (faceAiView == null) {
//            faceAiView = new ReemanFaceAIView(context);
//            LayoutParams mParams = new LayoutParams();
//            mParams.x = 1200;
//            mParams.y = 0;
//            mParams.type = LayoutParams.TYPE_SYSTEM_ALERT;
//            mParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE | LayoutParams.FLAG_FULLSCREEN;
//            mParams.format = PixelFormat.RGBA_8888;
////            mParams.width = 1;
////            mParams.height = 1;
//            mParams.width = 320;
//            mParams.height = 240;
//            mParams.gravity = Gravity.LEFT | Gravity.TOP;
//            windowManager.addView(faceAiView, mParams);
//        }
//    }
//
//    public static void removeReemacFaceAIView(Context context) {
//        if (faceAiView != null) {
//            Log.v("FloatWindowManager", "removefaceAiView");
//            getWindowManager(context).removeView(faceAiView);
//            faceAiView = null;
//        }
//    }

}
